package cn.nawang.ebeim.api.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import cn.nawang.ebeim.api.comm.BaseController;
import cn.nawang.ebeim.api.comm.PageList;
import cn.nawang.ebeim.api.comm.Pages;
import cn.nawang.ebeim.api.comm.SearchFormParams;
import cn.nawang.ebeim.api.service.SalesPackageService;
import cn.nawang.ebeim.api.utils.Constant;
import cn.nawang.ebeim.api.utils.DateOperate;
import cn.nawang.ebeim.api.utils.DateUtils;

/**
 * @ClassName: SalesPackageController
 * @Description: TODO
 * @author fanyb
 * @date Nov 4, 2015 9:31:20 AM
 * @version v1.0
 */
@RestController
public class SalesPackageController extends BaseController{
  private static final Logger LOG = LoggerFactory.getLogger(SalesPackageController.class); 
  
  @Autowired
  private SalesPackageService salesPackageService;
  
  @RequestMapping(value = "/sales_package/list", method = RequestMethod.GET)
  public Object findAllByPaging(Pages page, HttpServletRequest request) {
    LOG.debug("/sales_package/list {}",page);
     Map<String, Object> map = new HashMap<String, Object>();
    // 组装查询条件
    List<SearchFormParams> dtovect = new ArrayList<SearchFormParams>();
    // 套餐名称
    String salesPackageName = request.getParameter("salesPackageName");
    if (!StringUtils.isEmpty(salesPackageName)) {
      dtovect.add(new SearchFormParams("s.salesPackageName", salesPackageName, "salesPackageName",
          SearchFormParams.STRING_TYPE));
    }
    // 起始时间
    String startTime = request.getParameter("startTime");
    if (!StringUtils.isEmpty(startTime)) {
      Date date = DateOperate.formatStringToDate(startTime, DateOperate.DEFAULTSTYLE);
      dtovect.add(new SearchFormParams("s.createTime", date.getTime(), "startTime", SearchFormParams.START_TIME_TYPE));
    }
    
    // 结束时间
    String endTime = request.getParameter("endTime");
    if (!StringUtils.isEmpty(endTime)) {
      Date date = DateOperate.formatStringToDate(endTime, DateOperate.DEFAULTSTYLE);
      long endTimeLong=DateUtils.getDateEndTime(date);
      dtovect.add(new SearchFormParams("s.createTime", endTimeLong, "endTime", SearchFormParams.END_TIME_TYPE));
    }
    if (dtovect.size() > 0) {
      page.setSearchFormParams(dtovect.toArray(new SearchFormParams[dtovect.size() - 1]));
    }
    PageList pagelist = salesPackageService.findPageList(page);
    map.put(Constant.JSON_CODE, Constant.CODE_200);
    map.put("list", pagelist.getObjList());
    map.put("page", pagelist.getPage());
    LOG.debug("findAllByPaging {}", map);
    return map;
  }
}
